package com.shujia.spark.core

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo05MapValues {
  def main(args: Array[String]): Unit = {

    /**
     * mapValues：转换算子
     * 首先需要构建KV格式的RDD，然后可以通过该算子对KV的V进行操作
     */
    val conf: SparkConf = new SparkConf()

    conf.setAppName("Demo05MapValues")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val stuRDD: RDD[String] = sc.textFile("spark/data/stu/students.txt")

    // 将学生的姓名以及年龄取出来，再将年龄-1
    val nameAndAgeRDD: RDD[(String, Int)] = stuRDD
      .map(line => {
        val splits: Array[String] = line.split(",")
        (splits(1), splits(2).toInt)
      })

    nameAndAgeRDD.mapValues(i => i - 1).foreach(println)

  }

}
